using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

[RequireComponent(typeof(NetworkIdentity))]
public class EnemySpawner : NetworkBehaviour
{
	public bool AutoSpawn;

	public GameObject[] ObjectSpawn;

	public GameObject[] SubSpawner;

	public float TimeSpawn = 3f;

	public int MaxObject = 10;

	public string PlayerTag = "Player";

	public bool PlayerEnter = true;

	private float timetemp;

	private int indexSpawn;

	private List<GameObject> spawnList = new List<GameObject>();

	[HideInInspector]
	public bool OnActive;

	private void Start()
	{
		GetComponent<NetworkIdentity>().serverOnly = true;
		timetemp = Time.time;
	}

	private void Update()
	{
		OnActive = false;
		if (!base.isServer)
		{
			return;
		}
		if (PlayerEnter)
		{
			GameObject[] array = GameObject.FindGameObjectsWithTag(PlayerTag);
			for (int i = 0; i < array.Length; i++)
			{
				float num = Vector3.Distance(base.transform.position, array[i].transform.position);
				Vector3 localScale = base.transform.localScale;
				if (num < localScale.x)
				{
					OnActive = true;
				}
			}
		}
		else
		{
			OnActive = true;
		}
		if (OnActive && AutoSpawn && Time.time > timetemp + TimeSpawn)
		{
			Spawn();
			timetemp = Time.time;
		}
	}

	public GameObject[] Spawn(int number)
	{
		GameObject[] array = new GameObject[number];
		for (int i = 0; i < array.Length; i++)
		{
			array[i] = Spawn();
		}
		return array;
	}

	public GameObject Spawn()
	{
		GameObject gameObject = null;
		int num = ObjectSpawnedCount();
		indexSpawn = Random.Range(0, ObjectSpawn.Length);
		if (ObjectSpawn[indexSpawn] == null)
		{
			return null;
		}
		if (num < MaxObject)
		{
			if (SubSpawner.Length > 0)
			{
				int num2 = Random.Range(0, SubSpawner.Length);
				Vector3 position = SubSpawner[num2].transform.position;
				Vector3 localScale = SubSpawner[num2].transform.localScale;
				int min = -(int)(localScale.x / 2f);
				Vector3 localScale2 = SubSpawner[num2].transform.localScale;
				float x = Random.Range(min, (int)(localScale2.x / 2f));
				Vector3 localScale3 = SubSpawner[num2].transform.localScale;
				int min2 = (int)((0f - localScale3.z) / 2f);
				Vector3 localScale4 = SubSpawner[num2].transform.localScale;
				Vector3 position2 = DetectGround(position + new Vector3(x, 0f, Random.Range(min2, (int)(localScale4.z / 2f))));
				gameObject = UnitZ.gameNetwork.RequestSpawnObject(ObjectSpawn[indexSpawn].gameObject, position2, Quaternion.identity);
			}
			else
			{
				Vector3 position3 = base.transform.position;
				Vector3 localScale5 = base.transform.localScale;
				int min3 = -(int)(localScale5.x / 2f);
				Vector3 localScale6 = base.transform.localScale;
				float x2 = Random.Range(min3, (int)(localScale6.x / 2f));
				Vector3 localScale7 = base.transform.localScale;
				int min4 = (int)((0f - localScale7.z) / 2f);
				Vector3 localScale8 = base.transform.localScale;
				Vector3 position4 = DetectGround(position3 + new Vector3(x2, 0f, Random.Range(min4, (int)(localScale8.z / 2f))));
				gameObject = UnitZ.gameNetwork.RequestSpawnObject(ObjectSpawn[indexSpawn].gameObject, position4, Quaternion.identity);
			}
			if ((bool)gameObject)
			{
				spawnList.Add(gameObject);
			}
		}
		return gameObject;
	}

	private int ObjectSpawnedCount()
	{
		int num = 0;
		foreach (GameObject spawn in spawnList)
		{
			if (spawn != null)
			{
				num++;
			}
		}
		return num;
	}

	private void OnDrawGizmos()
	{
	}

	private Vector3 DetectGround(Vector3 position)
	{
		if (Physics.Raycast(position, -Vector3.up, out RaycastHit hitInfo, 1000f))
		{
			return hitInfo.point;
		}
		return position;
	}

	private void UNetVersion()
	{
	}

	public override bool OnSerialize(NetworkWriter writer, bool forceAll)
	{
		bool result = default(bool);
		return result;
	}

	public override void OnDeserialize(NetworkReader reader, bool initialState)
	{
	}
}
